import router from './router'
import store from './store'

const whiteList = ['/login'] // no redirect whitelist
router.beforeEach(async (to, from, next) => {
  const hasToken = sessionStorage.getItem('token')
  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      store.dispatch('getRouter').then(res => {
        next()
      })
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  // NProgress.done()
})
